<template>
  <div class="dashboard-container">
    <div class="app-container">
      <pageTools>
        <!-- 将按钮放在右边的布局组件 自己封装的 -->
        <template v-slot:after>
          <el-button type="primary" size="small" @click="addPermission(1,'0')">添加权限</el-button>
        </template>
      </pageTools>
      <!-- 需要注意的是， 如果需要树表， 需要给**el-table**配置**row-key**属性  id -->
      <el-table border="" :data="list" row-key="id">
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" align="center" label="标识" />
        <el-table-column prop="description" align="center" label="描述" />
        <el-table-column align="center" label="操作">
          <template v-slot="{row}">
            <el-button v-if="row.type === 1" type="text" @click="addPermission(2,row.id)">添加</el-button>
            <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
            <el-button type="text" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :title="Title" :visible="showDialog" @close="btnNo">
      <el-form ref="forms" label-width="120px" :model="formData" :rules="Rules">
        <el-form-item label="名称" prop="name">
          <el-input v-model="formData.name" style="width:80%" />
        </el-form-item>
        <el-form-item label="标识" prop="code">
          <el-input v-model="formData.code" style="width:80%" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="formData.description" style="width:80%" />
        </el-form-item>
        <el-form-item label="开关">
          <el-switch
            v-model="formData.enVisible"
            style="width:80%"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template v-solt="footer">
        <el-row type="flex" justify="center">
          <el-col :span="7">
            <el-button type="primary" size="small" @click="btnOk">确定</el-button>
            <el-button size="samll" @click="btnNo">取消</el-button>
          </el-col>
        </el-row>
      </template>

    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, delPermission, addPermission, getPermissionDetail, updatePermission } from '@/api/permisson'
import { tranListToTreeData } from '@/utils'
export default {
  name: '',
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        enVisible: '0', // 开启

        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '' // 因为做的是树 需要知道添加到哪个节点下了
      },
      Rules: {
        name: [{ required: true, message: '必填', trigger: 'blur' }],
        code: [{ required: true, message: '必填', trigger: 'blur' }]
      },
      Title: ''
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const data = await getPermissionList() // 数组数据  我们需要将数组数据转化为树形数据
      const treeNode = tranListToTreeData(data, '0') // 数组数据转化为树形数据
      this.list = treeNode
    },

    async delPermission(id) {
      try {
        await this.$confirm('确定删除该权限吗？')
        await delPermission(id) // 删除权限完成
        this.$message.success('删除权限完成')
        this.getPermissionList() // 然后重新拉取
      } catch (err) {
        console.log(err)
      }
    },

    // 当type为1时为访问权限，type为2时为功能权限
    addPermission(type, pid) { // 展示dialog弹层 并且记录下来这次的类型和pid 类型用于判断点的是“添加”还是“添加权限”
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
      this.Title = '新增权限'
    },

    async editPermission(id) {
      this.formData = await getPermissionDetail(id) // 根据id获取权限点的接口返回数据中，就会包含id属性 赋值给formData，那他也有了id属性
      this.showDialog = true
      this.Title = '编辑权限'
      // await updatePermission(this.formData)
      // this.showDialog = false
      // this.getPermissionList()
    },

    async btnOk() {
      try {
        await this.$refs.forms.validate() // 验证表单规则
        if (this.formData.id) {
          // 有id 说明此刻进行的是 编辑
          await updatePermission(this.formData)
        } else {
          await addPermission(this.formData) // 添加权限
        }
        this.$message.success('添加权限成功')
        this.getPermissionList()
      } catch (err) {
        console.log('验证规则出错')
      }
      this.showDialog = false
    },

    btnNo() {
      this.formData = {}
      this.$refs.forms.resetFields() // 移除掉验证规则
      this.showDialog = false
    }
  }
}
</script>

<style>

</style>
